﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace Golf.WebUI.AdminShow.SystemManagement
{
    public partial class ManagementZSG : Golf.WebUI.App_code.ValidateLogin
    {
        public string amdinName = "";
        public int adminId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                switch (Request.Params["action"])
                {
                    case "modify"://修改
                        ShowInfo();
                        break;
                    case "add":
                        _Add();
                        break;
                    case "del":
                        _Del();
                        break;
                    case "mofify_pwd":
                        mofify_pwd();
                        break;
                    case "updatePwd":
                        updatePwd();
                        break;
                }
            }
        }
        /// <summary>
        /// 修改密码ajax
        /// </summary>
        private void updatePwd()
        {
            string oldPwd = "";
            string NewPwd = "";
            int a_id = 0;
            if (Request.Params["oldPwd"] != null && Request.Params["oldPwd"].Trim() != "")
            {
                oldPwd = Request.Params["oldPwd"];
            }
            if (Request.Params["NewPwd"] != null && Request.Params["NewPwd"].Trim() != "")
            {
                NewPwd = Request.Params["NewPwd"];
            }
            if (Request.Params["adminid"] != null && Request.Params["adminid"].Trim() != "")
            {
                a_id = (Convert.ToInt32(Request.Params["adminid"]));
            }

            Golf.BLL.Golf_Admin bll = new Golf.BLL.Golf_Admin();

            Golf.Model.Golf_Admin model = bll.GetModel(a_id);
            if (model == null)
            {
                Response.Write("暂无记录 cookie可能失效 请重新登录|");
            }
            else
            {

                if (model.PassWord == ThumNail.Lower32(oldPwd))
                {
                    model.PassWord = ThumNail.Lower32(NewPwd);
                    if (bll.Update(model))
                    {
                        Golf.WebUI.AdminShow.Admin.Login login = new Golf.WebUI.AdminShow.Admin.Login();
                        login.logout(Request, false);
                        Response.Write("修改成功，系统已退出 请刷新页面|");
                    }
                    else
                    {
                        Response.Write("修改失败|");
                    }
                }
                else
                {
                    Response.Write("旧密码错误|");
                }
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        private void mofify_pwd()
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                lab_id.Text = Request.Params["id"];
                adminId = (Convert.ToInt32(Request.Params["id"]));
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        private void _Del()
        {
            Golf.BLL.Golf_Admin bll = new Golf.BLL.Golf_Admin();
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int Id = (Convert.ToInt32(Request.Params["id"]));
                if (bll.Delete(Id))
                {
                    Response.Write("成功删除,");
                }
                else
                {
                    Response.Write("删除失败,");
                }
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        private void _Add()
        {
            _action.Value = "add";
        }
        private void ShowInfo()
        {
            _action.Value = "modify";
            int Id = 0;
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                Id = (Convert.ToInt32(Request.Params["id"]));
            }
            Golf.BLL.Golf_Admin bll = new Golf.BLL.Golf_Admin();
            Golf.Model.Golf_Admin model = bll.GetModel(Id);
            this.lblId.Text = model.Id.ToString();
            this.txtAdminName.Text = model.AdminName;
            amdinName = model.AdminName;
            this.chkIsState.Checked = model.IsState;
            this.txtGroupId.Text = model.GroupId.ToString();
            this.txtRealName.Text = model.RealName;
            this.txtLoginTime.Text = model.LoginTime.ToString();
            this.txtLastLoginTime.Text = model.LastLoginTime.ToString();
            this.txtLoginIP.Text = model.LoginIP;
            this.txtLastLoginIP.Text = model.LastLoginIP;
            this.txtLoginNumber.Text = model.LoginNumber.ToString();
            this.txtAddTime.Text = model.AddTime.ToString();

            this.txtAddTime.Enabled = false;
            this.txtLoginNumber.Enabled = false;
            this.txtLastLoginIP.Enabled = false;
            this.txtLoginIP.Enabled = false;
            this.txtLastLoginTime.Enabled = false;
            this.txtLoginTime.Enabled = false;

        }

        public void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtAdminName.Text.Trim().Length == 0)
            {
                strErr += "用户名不能为空！\\n";
            }
            if (_action.Value == "add")
                if (this.txtPassWord.Text.Trim().Length == 0)
                {
                    strErr += "密码不能为空！\\n";
                }
            if (!PageValidate.IsNumber(txtGroupId.Text))
            {
                strErr += "0不具备任何权限 Golf_A格式错误！\\n";
            }
            if (this.txtRealName.Text.Trim().Length == 0)
            {
                strErr += "真实姓名不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtLoginTime.Text))
            {
                strErr += "登录时间格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtLastLoginTime.Text))
            {
                strErr += "最后登陆时间格式错误！\\n";
            }
            if (this.txtLoginIP.Text.Trim().Length == 0)
            {
                strErr += "登陆IP不能为空！\\n";
            }
            if (this.txtLastLoginIP.Text.Trim().Length == 0)
            {
                strErr += "最后登陆Ip不能为空！\\n";
            }
            if (!PageValidate.IsNumber(txtLoginNumber.Text))
            {
                strErr += "登陆次数格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtAddTime.Text))
            {
                strErr += "添加用户时间格式错误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string AdminName = this.txtAdminName.Text;
            string PassWord = this.txtPassWord.Text;
            bool IsState = this.chkIsState.Checked;
            int GroupId = int.Parse(this.txtGroupId.Text);
            string RealName = this.txtRealName.Text;
            DateTime LoginTime = DateTime.Parse(this.txtLoginTime.Text);
            DateTime LastLoginTime = DateTime.Parse(this.txtLastLoginTime.Text);
            string LoginIP = this.txtLoginIP.Text;
            string LastLoginIP = this.txtLastLoginIP.Text;
            int LoginNumber = int.Parse(this.txtLoginNumber.Text);
            DateTime AddTime = DateTime.Parse(this.txtAddTime.Text);

            Golf.BLL.Golf_Admin bll = new Golf.BLL.Golf_Admin();
            Golf.Model.Golf_Admin model = new Golf.Model.Golf_Admin();
            if (_action.Value == "add")
            {
                model.AdminName = AdminName;
                model.IsState = IsState;
                model.GroupId = GroupId;
                model.RealName = RealName;
                model.LoginTime = LoginTime;
                model.LastLoginTime = LastLoginTime;
                model.LoginIP = LoginIP;
                model.LastLoginIP = LastLoginIP;
                model.LoginNumber = LoginNumber;
                model.AddTime = AddTime;
                model.PassWord = ThumNail.Lower32(PassWord);//加密;
                if (bll.Add(model) > 0)
                {
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('添加成功！');", true);
                }
                else
                {

                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "ymPrompt.errorInfo('保存失败');", true);
                }
            }
            if (_action.Value == "modify")
            {
                int Id = int.Parse(this.lblId.Text);
                model = bll.GetModel(Id);
                model.AdminName = AdminName;
                model.IsState = IsState;
                model.GroupId = GroupId;
                model.RealName = RealName;
                model.LoginTime = LoginTime;
                model.LastLoginTime = LastLoginTime;
                model.LoginIP = LoginIP;
                model.LastLoginIP = LastLoginIP;
                model.LoginNumber = LoginNumber;
                model.AddTime = AddTime;
                if (bll.Update(model))
                {
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('修改成功！');", true);
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "", "ymPrompt.errorInfo('修改失败');", true);
                }
            }


        }
        public void btnCancle_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('用户已经取消操作！');", true);
        }
    }
}
